package com.yanqu.road.server.cmd.activity.huashigang;

import com.yanqu.road.entity.enums.activity.huashigang.eHuaShiGangEventType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.pb.huashigang.HuaShiGangProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

@Cmd(code = GameProtocol.S_HUA_SHI_GANG_AUTO_HANDLE_NEXT_POINT_FROM_CROSS, desc = "自动处理下个点 跨服返回")
public class AutoHandleNextPointFromCossCmd extends AbstractCommand {

    private int activityId;
    private HuaShiGangProto.AutoHandleNextPointRespMsg.Builder result;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        HuaShiGangProto.CrossAutoHandleNextPointRespMsg respMsg = HuaShiGangProto.CrossAutoHandleNextPointRespMsg.parseFrom(packet.getBytes());
        activityId = respMsg.getActivityId();
        result = respMsg.getRespMsg().toBuilder();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        // 贸易事件
        if (eHuaShiGangEventType.TRADE_EVENT.getValue() == result.getEventType()) {
            Property consume = PropertyHelper.parseStringToProperty(result.getConsume());
            Property reward = PropertyHelper.parseStringToProperty(result.getReward());
            if (player.getModule(CurrencyModule.class).removeCurrency(consume, eLogMoneyType.HuaShiGang, eLogMoneyType.HuaShiGangTrade)) {
                player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.HuaShiGang, eLogMoneyType.HuaShiGangTrade);
            } else {
                result.setReward(""); // 客户端靠这个判断是否成功
            }
        }
        // 宝库事件
        else if (eHuaShiGangEventType.TREASURE_EVENT.getValue() == result.getEventType() && result.getBoxStatus() == 0) {
            Property reward = PropertyHelper.parseStringToProperty(result.getReward());
            player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.HuaShiGang, eLogMoneyType.HuaShiGangOpenTreasure);
        }
    }

    @Override
    public void sendData(GamePlayer player) {
        player.sendPacket(ClientProtocol.U_HUA_SHI_GANG_AUTO_HANDLE_NEXT_POINT, result);
    }
}
